﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Collections;

namespace ViCode_LeVi.Controls.EnumsControl
{
    public partial class EnumControl_Base : UserControl
    {
        public EnumControl_Base()
        {
            InitializeComponent();
        }
        public EnumControl_Base(Func<IEnumerable> aFunc_GetDataSource, Func<object, object> aFunc_GetDisplay)
        {
            InitializeComponent();
            Func_GetDataSource = aFunc_GetDataSource;
            Func_GetDisplay = aFunc_GetDisplay;
        }
        bool _MultiSelect = false;
        public void Setup(Action<object> aAction_AfterChanged, bool aMultiSelect)
        {
            _MultiSelect = aMultiSelect;
            Action_AfterChanged = aAction_AfterChanged;
            StackPanel_TaskType_LoadData();
        }
        public Orientation Option_Orientation
        {
            get { return StackPanel_EnumControl_Base.Orientation;}
            set { StackPanel_EnumControl_Base.Orientation = value;}
        }
        public Func<IEnumerable> Func_GetDataSource;
        public Func<object, object> Func_GetDisplay;        
        public List<ToggleButton> List_CheckBox = new List<ToggleButton>();
        public Action<Object> Action_AfterChanged;
        public void StackPanel_TaskType_LoadData()
        {
            StackPanel_EnumControl_Base.Children.Clear();
            List_CheckBox.Clear();
            var data = Func_GetDataSource();
            foreach (var d in data)
            {
                ToggleButton chk = null;
                if (_MultiSelect == false)
                {
                    //GroupName = "VMTabAnalysis_Tab1_Win", 
                    chk = (new RadioButton() { DataContext = d, Content = Func_GetDisplay(d), IsChecked = false , Margin = new Thickness(2)});
                }
                else
                {
                    chk = (new CheckBox() { DataContext = d, Content = Func_GetDisplay(d), IsChecked = true, Margin = new Thickness(2) });
                }
                StackPanel_EnumControl_Base.Children.Add(chk);
                List_CheckBox.Add(chk);
                chk.Click -= new RoutedEventHandler(chk_Click);
                chk.Click += new RoutedEventHandler(chk_Click);

            }
            if (_MultiSelect == false)
                List_CheckBox[0].IsChecked = true;
            AfterChangedRun();
        }

        private void AfterChangedRun()
        {
            var data = List_CheckBox.Where(c => c.IsChecked == true).FirstOrDefault();
            if (Action_AfterChanged != null && data!= null)
                Action_AfterChanged(data.DataContext);
        }

        void chk_Click(object sender, RoutedEventArgs e)
        {
            AfterChangedRun();
        }
    }
}
